<?php

namespace forfun\controller;

use forfun\controller\vo\WeirdForm;
use forfun\repository\entity\Comment;
use forfun\service\CommentService;
use forfun\service\WeirdService;
use restphp\http\RestHttpRequest;
use forfun\controller\vo\CommentForm;

/**
 * @RequestMapping ("/api/v1/weirds")
 */
class ApiWeirdController extends ApiBaseController {
    /**
     * @Get ("")
     * @Logon
     * @return void
     */
    public function getList() {
        $key = RestHttpRequest::getPathValue('key');
        $page = RestHttpRequest::getRequest("page", 1);
        $range = RestHttpRequest::getRequest("range", 0);
        $this->_success(WeirdService::getWeirdList($key, $page, $range));
    }

    /**
     * @Post ("")
     * @Logon
     * @return void
     */
    public function add() {
        $weirdForm = RestHttpRequest::getBody(WeirdForm::class, true);
        WeirdService::add($weirdForm);
    }

    /**
     * @Put ("/{id}")
     * @Logon
     * @return void
     */
    public function modify() {
        $id = RestHttpRequest::getPathValue('id');
        $weirdForm = RestHttpRequest::getBody(WeirdForm::class, true);
        WeirdService::modify($id, $weirdForm);
    }

    /**
     * @Delete ("/{id}")
     * @Logon
     * @return void
     */
    public function delete() {
        $id = RestHttpRequest::getPathValue('id');
        WeirdService::delete($id);
    }

    /**
     * @Post ("/{id}/actions/favour")
     * @Logon
     * @return void
     */
    public function addFavour() {
        $id = RestHttpRequest::getPathValue('id');
        WeirdService::addFavour($id);
    }

    /**
     * @Post ("/{id}/comments")
     * @Logon
     * @return void
     */
    public function addComment() {
        $id = RestHttpRequest::getPathValue('id');
        $commentForm = RestHttpRequest::getBody(CommentForm::class, true);
        WeirdService::addComment($id, $commentForm->getComment());
    }

    /**
     * @Get ("/{id}/comments")
     * @Logon
     * @return void
     */
    public function getCommentList() {
        $id = RestHttpRequest::getPathValue('id');
        $page = RestHttpRequest::getRequest("page", 1);
        $this->_success(CommentService::getCommentList(Comment::KIND_WEIRD, $id, $page));
    }
}